<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use DB;

use App\Org\Image;

use Intervention\Image\ImageManagerStatic;

class GoodsController extends Controller
{
    public function index()
    {
    	$list = DB::table('goods')->get();
    	// dd($list);
    	return view('admin.goods.index',['list'=>$list]);
    }
    public function destroy($id)
    {
    	// dd($id);
    	$m = DB::table('goods')->where('goodsid',$id)->delete();
    	// dd($m);
    	if ($m>0) {
    		return redirect('/admin/goods');
    	}
    }
    public function create()
    {
    	return view('admin.goods.add');
    }
    public function store(Request $request)
    {
        if ($request->hasFile('picture')) {
            if ($request->file('picture')->isValid()) {
                $file = $request->file('picture');
                // dd($file);
                $ext = $file->getClientOriginalExtension();
                $picname = time().rand(1000,9999).'.'.$ext;
                // dd($picname);
                // 保存图片
                $file->move('./uploads/',$picname);
                 //自定义类进行图片缩放处理
                // $img = new Image();
                // $im = $img->open('./uploads/'.$picname);
                // $im->thumb(160,110)->save('./uploadss/'.$picname);
            }
        }
    	//自动规则
    	// dd($request);
    	$messages = [
    		'required'=>':attribute是必须填的',
    	];
    	$this->validate($request,[
    		'goodsname' => 'required|unique:goods',
            'intro' => 'required',
            'price' => 'required',
            'stock' => 'required',
    		],$messages);
    	//处理图片
        // dd($im);
    	$subtitle = $request->input('subtitle');
    	if (empty($subtitle)) {
    		$data = $request->except('_token','subtitle');
    	}else{
    		$data = $request->except('_token');
    	}

        $preferential = $request->input('preferential');
        // dd($preferential);
    	if (empty($preferential)) {
    		$data = $request->except('_token','preferential');
            // dd($data);
    	}else{
    		$data = $request->except('_token');
            // dd($data);
    	}

    	if (empty($subtitle) && empty($preferential)) {
    		$data = $request->except('subtitle','_token','preferential');
            // dd($data);
    	}
        if (!empty($subtitle) && !empty($preferential)) {
            $data = $request->except('_token');
        }
    	// dd($data);
    	$m = DB::table('goods')->insertGetId($data);
		    	// dd($m);
		   if ($m>0) {
		    return redirect('/admin/goods');
		   }else{
            return back();
           }
    }
    //修改部分
    public function edit($id)
    {
    	$list = DB::table('goods')->where('goodsid',$id)->first();
    	// dd($list);
    	return view('admin.goods.edit',['list'=>$list]);
    }
    public function update($id,Request $request)
    {
    	if ($request->hasFile('picture')) {
    		if ($request->file('picture')->isValid()) {
			    $file = $request->file('picture');
			    // dd($file);
			    $ext = $file->getClientOriginalExtension();
			    $picname = time().rand(1000,9999).'.'.$ext;
		        // dd($picname);
		        // 保存图片
			    $file->move('./uploads/',$picname);
			     //自定义类进行图片缩放处理
		        // $img = new Image();
		        // $im = $img->open('./uploads/'.$picname);
		        // $im->thumb(160,110)->save('./uploadss/'.$picname);
			}
		}
        // dd($im);
    	$subtitle = $request->input('subtitle');
    	// dd($subtitle);
    	if (empty($subtitle)) {
    		$data = $request->except('_token','_method','subtitle');
    	}else{
    		$data = $request->except('_token','_method');
    	}

    	 $preferential = $request->input('preferential');
        // dd($preferential);
        if (empty($preferential)) {
            $data = $request->except('_token','_method','preferential');
        }else{
            $data = $request->except('_token','_method');
        }

        if (empty($subtitle) && empty($preferential)) {
            $data = $request->except('subtitle','_token','_method','preferential');
        }
        if (!empty($subtitle) && !empty($preferential)) {
            $data = $request->except('_token','_method');
        }
        // 修改购物车的价格
        $price = $request->input('price');
        $car = DB::table('car')->get();
        foreach ($car as $k => $v) {
            if ($id == $v->goodsid ) {
                $carda = array('goodsprice'=>$price);
                DB::table('car')->where('goodsid',$id)->update($carda);
            }
        }
        //修改商品分类的名字
        $class = DB::table('goods')->where('goodsid',$id)->get();
        $goodsname = $class[0]->goodsname;
        $classname = $request->input('goodsname');
        $one = DB::table('classone')->get();
        foreach ($one as $k => $v) {
           if ($goodsname == $v->classname ) {
                $oneid = $v->oneid;
                $oneda = array('classname'=>$classname);
                DB::table('classone')->where('oneid',$oneid)->update($oneda);
            }
        }
        $odd = DB::table('classodd')->get();
        foreach ($odd as $k => $v) {
           if ($goodsname == $v->classname ) {
                $oddid = $v->oddid;
                $oddda = array('classname'=>$classname);
                DB::table('classodd')->where('oddid',$oddid)->update($oddda);
            }
        }
        $even = DB::table('classeven')->get();
        foreach ($even as $k => $v) {
           if ($goodsname == $v->classname ) {
                $evenid = $v->evenid;
                $evenda = array('classname'=>$classname);
                DB::table('classeven')->where('evenid',$evenid)->update($evenda);
            }
        }
        $m = DB::table('goods')->where('goodsid',$id)->update($data);
    	if ($m>0) {
    		return redirect('/admin/goods');
    	}else{
            return redirect('/admin/goods');
        }
    }
}
